﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.NTD
{
    public partial class DangTuyenDungBuoc3 : System.Web.UI.Page
    {
        public int Tuyendungid { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            CKTTLHMoTa.Language = "vi";            
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/ckfinder/";
            _FileBrowser.SetupCKEditor(CKTTLHMoTa);
            //            
            if (Session["NTD"] == null)
            {
                Response.Redirect("/nha-tuyen-dung.html");
                return;
                
            }
            if(Request.QueryString["id"]!=null)
            {
                Tuyendungid = int.Parse(Request.QueryString["id"]);
            }
            if(!IsPostBack)
            {
                
                LoadData();
            }
        }
        public void LoadData()
        {
            DAL.tbl_TD_TuyenDung tuyenDung = BLL.TuyenDung.DangTuyenHelper.Get_TinTuyenDung(Tuyendungid); 
            DAL.UserTuyenDung userTuyenDung = BLL.TuyenDung.UserHelper.Get_UserEmail(Session["NTD"].ToString());
            #region loadata            
            ListItem item = new ListItem();
            item.Text = "--Chọn quốc gia--";
            item.Value = "-1";
            item.Selected = true;
            droNgonNguHoSo.DataSource = BLL.Admin.QuocGiaHelper.List_QuocGia();
            droNgonNguHoSo.DataTextField = "Ten";
            droNgonNguHoSo.DataValueField = "QuocGiaId";
            droNgonNguHoSo.DataBind();
            droNgonNguHoSo.Items.Add(item);
            //
            droQuocGia.DataSource = BLL.Admin.QuocGiaHelper.List_QuocGia();
            droQuocGia.DataTextField = "Ten";
            droQuocGia.DataValueField = "QuocGiaId";
            droQuocGia.DataBind();            
            //
            droTinhThanhPho.DataSource = BLL.Admin.TinhHelper.List_Tinh_Theo_QuocGia(int.Parse(droQuocGia.SelectedValue));
            droTinhThanhPho.DataTextField = "Ten";
            droTinhThanhPho.DataValueField = "TinhId";
            droTinhThanhPho.DataBind();
            //
            txtDiaChi.Text = userTuyenDung.DiaChi;
            txtDienThoai.Text = userTuyenDung.DienThoaiLienHe;
            txtEmailLienHe.Text = userTuyenDung.EmailLienHe;
            txtFax.Text = userTuyenDung.Fax;
            txtNguoiLienHe.Text = userTuyenDung.NguoiLienHe;
            txtNgayGui.Text = tuyenDung.NgayDang.Value.ToShortDateString();
            #endregion
            #region loadupdate
            if(tuyenDung.CachLienHe!=null)
            {
                string[] chuoi = tuyenDung.CachLienHe.Split(';');
                foreach (var s in chuoi)
                {
                    if(s=="1")
                    {
                        checkNopTrucTuyen.Checked = true;
                    }
                    if (s=="2")
                    {
                        checkTrucTiep.Checked = true;
                    }
                    if (s == "3")
                    {
                        checkQuaMail.Checked = true;
                    }
                    
                }
            }
            if(tuyenDung.NgonNguHoSo!=null)
            {
                droNgonNguHoSo.SelectedValue = tuyenDung.NgonNguHoSo.ToString();
            }
            if(tuyenDung.TTLHMoTa!=null)
            {
                CKTTLHMoTa.Text = tuyenDung.TTLHMoTa;
            }
            if(tuyenDung.DiaChi!=null)
            {
                txtDiaChi.Text = tuyenDung.DiaChi;
            }
            if(tuyenDung.QuocGiaId!=null)
            {
                droQuocGia.SelectedValue = tuyenDung.QuocGiaId.ToString();
                droTinhThanhPho.DataSource = BLL.Admin.TinhHelper.List_Tinh_Theo_QuocGia(int.Parse(droQuocGia.SelectedValue));
                droTinhThanhPho.DataTextField = "Ten";
                droTinhThanhPho.DataValueField = "TinhId";
                droTinhThanhPho.DataBind();
            }
            if (tuyenDung.TinhId != null)
            {
                droTinhThanhPho.SelectedValue = tuyenDung.TinhId.ToString();
            }
            if (tuyenDung.TenLienHe != null)
            {
                txtTenLienHe.Text = tuyenDung.TenLienHe;
            }
            if (tuyenDung.DienThoai != null)
            {
                txtDienThoai.Text = tuyenDung.DienThoai;
            }
            if (tuyenDung.Fax != null)
            {
                txtFax.Text = tuyenDung.Fax;
            }
            if (tuyenDung.EmailLienHe != null)
            {
                txtEmailLienHe.Text = tuyenDung.EmailLienHe;
            }
            if (tuyenDung.NguoiLienHe != null)
            {
                txtNguoiLienHe.Text = tuyenDung.NguoiLienHe;
            }
            if (tuyenDung.NgayDang != null)
            {
                txtNgayGui.Text = tuyenDung.NgayDang.Value.ToShortDateString();
            }
            if (tuyenDung.CachHienThi != null)
            {
                droCachHienThi.SelectedValue = tuyenDung.CachHienThi.ToString();
            }
            if (tuyenDung.PhuongThucThanhToan != null)
            {
                droCachHienThi.SelectedValue = tuyenDung.PhuongThucThanhToan.ToString();
            }
            droThoiGian.SelectedValue = (tuyenDung.NgayHetHan.Value - tuyenDung.NgayDang.Value).TotalDays.ToString();
            #endregion
        }
        public void DangTuyenDungBuoc_3()
        {
            string cachLienHe ="";
            if(checkNopTrucTuyen.Checked==true)
            {
                cachLienHe += "1;";
            }
            if (checkTrucTiep.Checked == true)
            {
                cachLienHe += "2;";
            }
            if (checkQuaMail.Checked == true)
            {
                cachLienHe += "3;";
            }
            if(droNgonNguHoSo.SelectedValue=="-1")
            {
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa chọn ngôn ngữ nhận hồ sơ!');", true);
                return;
            }
            if (droTinhThanhPho.Items.Count < 1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa có dữ liệu tỉnh thành phố!');", true);
                return;
            }
            DateTime ngayDang = DateTime.Parse(txtNgayGui.Text);
            DateTime ngayHetHan = ngayDang.AddDays(int.Parse(droThoiGian.SelectedValue));
            BLL.TuyenDung.DangTuyenHelper.DangTuyenDungBuoc3(Tuyendungid,cachLienHe,int.Parse(droNgonNguHoSo.SelectedValue),CKTTLHMoTa.Text,txtDiaChi.Text,int.Parse(droQuocGia.SelectedValue),int.Parse(droTinhThanhPho.SelectedValue),txtTenLienHe.Text,txtDienThoai.Text,txtFax.Text,txtEmailLienHe.Text,txtNguoiLienHe.Text,ngayDang,ngayHetHan,int.Parse(droCachHienThi.SelectedValue),int.Parse(droPhuongThucThanhToan.SelectedValue));
            

        }
        protected void btLuuTam_Click(object sender, EventArgs e)
        {
            DangTuyenDungBuoc_3();
            Response.Redirect("/nha-tuyen-dung/viec-lam.html");
        }

        protected void btBuocTiep_Click(object sender, EventArgs e)
        {
            DangTuyenDungBuoc_3();
            Response.Redirect("/nha-tuyen-dung/" + Tuyendungid + "dang-tuyen-dung-buoc4.html");
        }

        protected void droQuocGia_SelectedIndexChanged(object sender, EventArgs e)
        {
            droTinhThanhPho.DataSource = BLL.Admin.TinhHelper.List_Tinh_Theo_QuocGia(int.Parse(droQuocGia.SelectedValue));
            droTinhThanhPho.DataTextField = "Ten";
            droTinhThanhPho.DataValueField = "TinhId";
            droTinhThanhPho.DataBind();
            droQuocGia.Focus();
        }
    }
}